package main

import (
	"log"
	"net/http"
	"net/http/httputil"
)

const (
	host = "47.99.154.221"
)

var (
	Host = map[string]string{
		"47.99.154.221":        host + ":10001", // 没有备案之前的测试
		"liangchao.top":        host + ":10001",
		"www.liangchao.top":    host + ":10001",
		"blogo.liangchao.top":  host + ":10001",
		"cosole.liangchao.top": host + ":10002",
	}
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.Host == "43.134.200.163" {
			return
		}
		director := func(req *http.Request) {
			req.URL.Scheme = "http"
			for k, v := range Host {
				if r.Host == k {
					req.URL.Host = v
					req.Host = v
					log.Println(k, "forward to ", v)
					break
				}
			}

		}
		proxy := &httputil.ReverseProxy{Director: director}
		proxy.ServeHTTP(w, r)
	})

	log.Println("http 代理启动中")
	for k, v := range Host {
		log.Printf("代理 %v ==> %v", k, v)
	}
	err := http.ListenAndServe(":80", nil)
	log.Fatal(err)
}
